<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Adds the new event(news) including a poster.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Inherited from Addaction.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_addaction.php';

require_once INSTALLDIR . '/widgets/slh_form_addeventnews.php';

require_once INSTALLDIR . '/classes/EventNews.php';

class AddeventnewsAction extends AddAction
{
	function title()
	{
		return '添加活动(新鲜事)';
	}
	
	function prepare($args)
	{
		if ( !Rights::hasRight(slh_get_userrights(), RIGHT_NEWS)) {
			return false;
		}
		
		return parent::prepare($args);
	}
	
	/**
	 * Add the date_input/jcrop stylesheet.
	 */
	function showStylesheets()
	{
		parent::showStylesheets();
		
		$this->cssLink('theme/base/css/jquery.date_input.css', 'base', 'screen, projection, tv');
		
		$this->cssLink('theme/base/css/jquery.Jcrop.css', 'base', 'screen, projection, tv');
		$this->cssLink('theme/base/css/action/addeventnews.css');
	}
	
	/**
	 * Add the ajaxfileupload/date_input/jcrop/editor scripts.
	 */
	function showScripts()
	{
		parent::showScripts();
	
		$this->script('ajaxfileupload/jquery.ajaxfileupload.min.js');
		
		$this->script('date_input/jquery.date_input.min.js');
		
		$this->script('jcrop/jquery.Jcrop.min.js');
		
		$this->script('kindeditor/kindeditor.min.js');
		
		$this->script('action/addeventnews.go.js');
	}
	
	/**
	 * Shows the guidance.
	 * Overriding.
	 */
	function showGuidance()
	{
		// Html code here.
		$this->element('h3', null, '添加一个活动(新鲜事)');
		
		$this->instruction('呃,说明下添加的具体是什么东东,怎么添加好了.');
		
		// If added successfully, then?
		if ($this->added) {
			$this->element('p', 'cls_p_success', '添加成功!');
			// Or show a nav.
		}
	}
	
	/**
	 * Shows the widgets like form, or etc.
	 * Overriding.
	 */
	function showForm()
	{
		$form = new AddeventnewsForm($this, $this->args);
		$form->show();
	}	
	
	/**
	 * Try to add the event news into the database.
	 */
	function tryAdd()
	{
		// CSRF protection and Refreshing-post protection.
		$this->checkSessionToken();
		
		// The original temp file.
		$originalname	= $this->getTrimmedArg('originalname');

		$association_ids= $this->getTrimmedArg('association_ids');
		$poster_id		= $this->getTrimmedArg('poster_id');
		$title			= $this->getTrimmedArg('title');
		$message		= $this->getTrimmedArg('message');
		$host			= $this->getTrimmedArg('host');
		$address		= $this->getTrimmedArg('address');
		$started_date	= $this->getTrimmedArg('started_date');
		$started_hour	= $this->getTrimmedArg('started_hour');
		$started_minute	= $this->getTrimmedArg('started_minute');
		$ended_date		= $this->getTrimmedArg('ended_date');
		$ended_hour		= $this->getTrimmedArg('ended_hour');
		$ended_minute	= $this->getTrimmedArg('ended_minute');
		
		$rendered		= $this->getArg('rendered');
		
		$started 		= sprintf("%s %s:%s", $started_date, $started_hour, $started_minute);
		$ended 			= sprintf("%s %s:%s", $ended_date, $ended_hour, $ended_minute);

// TODO: Validates the data.
		try {
			if (empty($title)) {
				throw new Exception('请输入活动的名称.');
			} else if (empty($message)) {
				throw new Exception('请输入活动的简介.');
			} else if (empty($host)) {
				throw new Exception('请输入活动的组织方.');
			} else if (empty($address)) {
				throw new Exception('请输入活动的地点.');
			} else if ((empty($started_date)) || (!slh_validate_datetimestring($started))) {
				throw new Exception('请输入正确的活动开始时间.');
			} else if ((empty($ended_date)) || (!slh_validate_datetimestring($ended))) {
				throw new Exception('请输入正确的活动结束时间.');
			} else if (strtotime($started) > strtotime($ended)) {
				throw new Exception('开始的时间比结束时间还晚?坑爹呢亲.');
			} else if (mb_strlen($message) > EVENTNEWS_MESSAGE_MAXLENGHT) {
				throw new Exception('活动简介过长.');
			} else if ( (!empty($originalname)) && (empty($poster_id))) {
				// The poster is failed to be cropped or encounters other interruption.
				throw new Exception('请重新完成海报的上传或剪裁,上传完记得按剪裁按钮啊亲.');
			} else {
				if (get_magic_quotes_gpc()) {
					$rendered = stripslashes($rendered);
				}
				$rendered_encode = slh_compress($rendered);
				
				// Adds into the database.
				$eventnews_id = EventNews::add(array('association_ids' => $association_ids,
												 	 'poster_id' => $poster_id,
											 		 'title' => $title,
													 'message' => $message,
											 		 'host' => $host,
											 		 'address' => $address,
													 'started' => $started,
													 'ended' => $ended,
													 'rendered' => $rendered_encode,
													 'user_id' => slh_get_userid()));
				
				if ( !$eventnews_id) {
					throw new Exception('添加活动新鲜事失败!');
				}
				
				// Success.
				// Unset the seesion token preventing post-again.
				$this->unsetSessionToken();
				
				// Unset the seted values.
				unset($this->args['originalname']);
				unset($this->args['association_ids']);
				unset($this->args['poster_id']);
				unset($this->args['title']);
				unset($this->args['message']);
				unset($this->args['host']);
				unset($this->args['address']);
				unset($this->args['started']);
				unset($this->args['ended']);
				unset($this->args['rendered']);
				
				$this->added = true;
				$this->showAction();
			}
		} catch (Exception $e) {
			// See the uploadedposter_id is existed already, then clear it up!
			Poster::removeIfAiredPoster($poster_id);
			
			// Delete the temp original file.
			@unlink(Poster::path($originalname));
			
			$this->showAction($e->getMessage());
		}
	}
}

?>